<template>
    <div class="supplier-index yuan-container">
        <a-card title="供应商管理">
            <div class="filter-panel">
                <a-input-search class="filter-list" allowClear style="width: 200px;color: #05b089" v-model="keywords"
                    placeholder="供应商名称" @search="resetList" />
                <div class="filter-list" style="float: right;">
                    <a-button type="primary" @click="add" v-if="$utilCheckPermission(['supplierManagementAdd'])">新增</a-button>
                    <!-- <a-button type="primary" @click="add">下载导入模板</a-button>
                    <a-upload name="file" :multiple="false" :action="$config.baseURL + '/assetManage/importData'"
                        :showUploadList="false" @change="uploadChange">
                        <a-button type="primary">导入</a-button>
                    </a-upload> -->

                </div>
            </div>
            <div class="table">
                <a-table bordered :rowKey="item=> item.id" :data-source="pageList" :columns="columns" :pagination="pagination"
                    :loading="loading">
                </a-table>
            </div>
        </a-card>
        <Add :visible="addVisible" @closeModal="() => addVisible = false" :infoId="infoId" @resetList="getList" />
        <Detail :visible="detailVisible" @closeModal="() => detailVisible = false" :infoId="infoId" @resetList="getList" />
    </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { h } from "vue";
import nextApi, { SupplierListRes } from "@/api/nextApi";
import Add from "./components/Add.vue";
import Detail from "./components/Detail.vue";
@Component({
    name: "supplierIndex",
    components: { Add, Detail },
})
export default class extends Vue {
    keywords = "";
    loading = false;
    pagination = {
        pageSize: 10,
        current: 1,
        total: 0,
        showTotal: (total: number) => `共${total}条`,
        onChange: this.pageChange,
    };
    pageList: SupplierListRes[] = [];
    columns = [
        {
            title: "序号",
            dataIndex: "index",
            key: "index",
            align: "center",
            customRender: (text: string, record: any, index: number) => `${index + 1}`,
        },
        {
            align: "center",
            dataIndex: "name",
            key: "name",
            title: "供应商名称",
        },
        {
            align: "center",
            dataIndex: "contact",
            key: "contact",
            title: "联系人",
        },
        {
            align: "center",
            dataIndex: "phone",
            key: "phone",
            title: "联系方式",
        },
        {
            align: "center",
            dataIndex: "supplyQuantity",
            key: "supplyQuantity",
            title: "供货累计数量",
        },
        {
            align: "center",
            dataIndex: "state",
            key: "state",
            title: "状态",
            customRender: (text: string, record: SupplierListRes, index: number) => {
                const stateBox = h(
                    "div",
                    { class: record.state === 1 ? "c-success" : "c-danger" },
                    record.state === 1 ? "启用" : "禁用"
                );
                return stateBox;
            },
        },
        {
            align: "center",
            dataIndex: "action",
            key: "action",
            title: "操作",
            width: 260,
            customRender: (text: string, record: SupplierListRes, index: number) => {
                const detailBtn = h(
                    "a-button",
                    {
                        style: { marginRight: "5px" },
                        props: { icon: "eye", type: "primary", size: "small" },
                        on: { click: () => this.showDetail(record) },
                    },
                    "详情"
                );
                const editBtn = h(
                    "a-button",
                    {
                        style: { marginRight: "5px" },
                        props: { icon: "edit", type: "primary", size: "small" },
                        on: { click: () => this.editDetail(record) },
                    },
                    "编辑"
                );

                const statusDom = h(
                    "a-popconfirm",
                    {
                        props: {
                            title: record.state === 1 ? "确定要禁用吗？" : "确定要启用吗？",
                            okText: "确定",
                            cancelText: "取消",
                        },
                        on: { confirm: () => this.clickDisable(record) },
                    },
                    [
                        h(
                            "a-button",
                            {
                                props: {
                                    icon: record.state === 1 ? "delete" : "tool",
                                    type: record.state === 1 ? "danger" : "primary",
                                    size: "small",
                                },
                            },
                            record.state === 1 ? "禁用" : "启用"
                        ),
                    ]
                );
                const btn:any[] = [];
                if (this.$utilCheckPermission(["supplierManagementCompile"])) btn.push(editBtn);
                if (this.$utilCheckPermission(["supplierManagementDetails"])) btn.push(detailBtn);
                if (this.$utilCheckPermission(["supplierManagementForbidden"])) btn.push(statusDom);
                return h("div", btn);
            },
        },
    ];

    getRowKey(item: any) {
        return item.id;
    }

    async getList() {
        this.pageList = [];
        try {
            this.loading = true;
            const res = await nextApi.getSupplierList({
                name: this.keywords,
                pageSize: this.pagination.pageSize,
                pageNum: this.pagination.current,
            });
            this.pagination.total = res.data?.total || 0;
            this.pageList = res.data?.rows || [];
        } finally {
            this.loading = false;
        }
    }
    resetList() {
        this.pagination.current = 1;
        this.getList();
    }
    detailVisible = false;
    showDetail(item: SupplierListRes) {
        this.infoId = item.id;
        this.detailVisible = true;
    }

    addVisible = false;
    infoId = "";
    editDetail(item: SupplierListRes) {
        this.infoId = item.id;
        this.addVisible = true;
    }
    add() {
        this.infoId = "";
        this.addVisible = true;
    }
    pageChange(page: number, pageSize: number) {
        this.pagination.pageSize = pageSize;
        this.pagination.current = page;

        this.getList();
    }
    async clickDisable(item: SupplierListRes) {
        try {
            this.loading = true;
            await nextApi.editSupplier({
                id: item.id,
                state: item.state === 1 ? 2 : 1,
            });
        } finally {
            this.loading = false;
        }
        this.getList();
    }
    async mounted() {
        this.getList();
    }
    uploadChange(file: any) {
        if (file?.file?.response?.data.length) {
            this.$message.success(file?.file?.response?.msg);
            this.resetList();
        }
    }
}
</script>
<style lang="scss">
.supplier-index {
    padding: 20px;
}
</style>
